<?php
/**
 * Classe Liste de codes promos
 *
 * Description de la classe
 *
 * @author Mathieu
 */
class ListReductionCode extends Object {
    protected $id;
    protected $nom;
    protected $date_valid;
    protected $reduction;
    protected $nb_code_total;
    protected $min_achat;
	protected $utilisation;
	protected $creation;
	
	protected $codes;
	
	function __construct($id="", $params=array()) {
        if($id!='') {
            $this->id = $id;
            foreach($params as $key => $p) {
                $this->$key = $p;
            }
        }
    }
	function __toString() {
        if(!isset($this->nom)) $this->load();
        return $this->nom;
    }
	
	public function load() {
        $db = Database::getInstance();
        $result = $db->query("SELECT * FROM liste_code WHERE id = '".$this->id."'");
        if(sizeof($result) > 0) {
            foreach($result[0] as $key => $p) {
                $this->$key = $p;
            }
            return;
        }
        if(DEBUG) echo("Objet inconnu");
        return false;
    }
	
	public function getAllCodes(){
		$db = Database::getInstance();
		$result = $db->query("SELECT * FROM code_promo WHERE id_liste='".$this->id."'");
		$this->codes=array();
		foreach($result as $line){
			$this->codes[] = new ReductionCode($line['id'],$line);
		}
		return $this->codes;
	}
	
}